@Override public E pop(){ return array.removeLast(); }
-------------------------------------------------------- public E remove(int index){ if(index < 0 || index >= size) thrownew IllegalArgumentException("Remove failed. Index is illegal.");
E ret = data[index]; for(int i = index + 1 ; i < size ; i ++) data[i - 1] = data[i]; size --; data[size] = null; // loitering objects != memory leak
// 从数组中删除最后一个元素, 返回删除的元素 public E removeLast(){ return remove(size - 1); }
6 弹出栈定元素Peek() array.getLast();
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
@Override public E peek(){ return array.getLast(); } -------------------------------------------------------------------------------------- // 获取index索引位置的元素 public E get(int index){ if(index < 0 || index >= size) thrownew IllegalArgumentException("Get failed. Index is illegal."); return data[index]; }
public class LoopQueue<E> implements Queue<E> { private E[]data; private int front; private int tail; public LoopQueue(int capacity){ data = (E[])new Object[capacity+1]; } public LoopQueue(){ this(10); } @Override public int getSize(){ if(tail<front) return data.length-(front-tail); else{ return tail-front; } }
public int getCapacity(){ return data.length-1; } @Override public boolean isEmpty(){ return getSize()==0; }